﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BizScore.Model;

namespace BizScore
{

    /// <summary>
    /// 数据处理
    /// </summary>
    public class DataHandle
    {


        protected static DataHandle dataHandle = new DataHandle();


        protected List<PostFinaNumber> listPostInfo = new List<PostFinaNumber>();

        protected List<ExamScore> listExamScore = new List<ExamScore>();

        protected List<ExamScore> listPassExam = new List<ExamScore>();

        protected GetExamScore getExamScore = null;

        protected GetPostFinalNumber getPostInfo = null;


        public static DataHandle GetInstance()
        {
            return dataHandle;
        }


        /// <summary>
        /// 设置岗位信息目录
        /// </summary>
        /// <param name="path">绝对目录地址</param>

        public void SetPostInfoPath(string path)
        {

            if(!File.Exists(path))
            {
                throw new Exception("找不到岗位信息表");
            }

            getPostInfo = GetPostFinalNumber.GetInstance(path);
        }


        /// <summary>
        /// 设置考试信息表信息
        /// </summary>
        /// <param name="path">绝对目录地址</param>

        public void SetExamScorePath(string path,string saveAsPath)
        {
            if (!File.Exists(path))
            {
                throw new Exception("找不到考试信息表");
            }


            getExamScore = GetExamScore.GetInstance(path, saveAsPath);

        }



        /// <summary>
        /// 数据处理
        /// </summary>
        public async void HandleDataAsync()
        {
           await Task.Run(() => {
               HandleData();
            });
        }


        public void HandleData()
        {
            Office.Excel.KillExcelProcess();

            listPostInfo.Clear();
            listExamScore.Clear();
            listPassExam.Clear();

            getPostInfo.OpenExcel();
            getExamScore.OpenExcel();
            


            //获得岗位数据
            listPostInfo.AddRange(getPostInfo.GetAllPostInfo());

            Office.Excel.CloseAllExcel();

        //    Office.Excel.KillExcelProcess();

            //获得成绩数据
            listExamScore.AddRange(getExamScore.GetAllExamineeInfo());


            //查询
            var values =
                from val in (
                from postInfo in listPostInfo
                join exam in listExamScore on postInfo.PostID equals exam.PostID
                select new
                {
                    postInfo = postInfo,
                    exam = exam
                })
                group val by val.exam.PostID into newValue
                orderby newValue.Key
                select new
                {
                    postID = newValue.Key,
                    exams = (
                    from newVal in newValue
                    orderby newVal.exam.Total descending
                    select newVal

                    )
                };



            foreach (var vs in values)
            {
                int i = 0;
                float score = 0f;
                foreach (var v in vs.exams)
                {

                    if (v.exam.Total <= 0)
                    {
                        break;
                    }

                    if ((i + 1) == v.postInfo.FinalNumber * 2)
                    {
                        score = v.exam.Total;
                    }

                    if (v.exam.Total >= score)
                    {
                        listPassExam.Add(v.exam);
                    }
                    else
                    {
                        break;
                    }

                    i++;
                }

            }


            //写入新表中
            getExamScore.WriteAllExamineeInfo(listPassExam);


            getPostInfo.CloseExcel();
            getExamScore.CloseExcel();

            Office.Excel.CloseAllExcel();





        }


    }
}
